package com.sdau.repairmaster.UIFile;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.sdau.repairmaster.R;

public class ForgetPwdActivity_one extends AppCompatActivity implements View.OnClickListener{

    private ImageView ivBack;//返回按钮
    private EditText etPhoneNumber_hint;//展示手机号暗码
    private EditText etPhoneNumber;//获取手机号明码
    private ImageView ivPhoneNumberDel;//清空手机号
    private Button btSubmit;


    private String phone;//手机号

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd_one);


        initView();//初始化控件
        registerListener();//绑定监听器
    }
    //初始化控件
    public void initView(){
        ivBack =findViewById(R.id.ib_navigation_back);
        etPhoneNumber = findViewById(R.id.et_retrieve_tel);
        etPhoneNumber_hint = findViewById(R.id.et_retrieve_tel_hint);
        ivPhoneNumberDel = findViewById(R.id.iv_retrieve_tel_del);
        btSubmit = findViewById(R.id.bt_retrieve_submit);

        phone = getIntent().getStringExtra("phone");
        String phone_hint = phone.substring(0,3) + "*****" + phone.substring(8,11);
        etPhoneNumber_hint.setText(phone_hint);
        etPhoneNumber_hint.setEnabled(false);
    }

    //注册监听器
    public void registerListener(){
        //注册点击事件
        ivBack.setOnClickListener(ForgetPwdActivity_one.this);
        ivPhoneNumberDel.setOnClickListener(ForgetPwdActivity_one.this);
        btSubmit.setOnClickListener(ForgetPwdActivity_one.this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ib_navigation_back:
                finish();
                break;
            case R.id.iv_retrieve_tel_del:
                etPhoneNumber.setText(null);
                break;
            case R.id.bt_retrieve_submit:
                comparePhone();
                break;

        }
    }
    public void comparePhone(){
        String phone_num = etPhoneNumber.getText().toString().trim();

        if(phone_num.length() != 11) {
            Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
            return ;
        }


        if(phone_num.equals(phone)){
            startActivity(new Intent(ForgetPwdActivity_one.this,
                    ForgetPwdActivity_two.class).putExtra("phone",phone));
        }
        else{
            Toast.makeText(this, "手机号码错误校验错误", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(ForgetPwdActivity_one.this,
                   LoginActivity.class));
        }
    }
}